package com.fangshuoit.controller;

import com.fangshuoit.VO.ProductInfoVO;
import com.fangshuoit.VO.ProductVO;
import com.fangshuoit.VO.ResultVO;
import com.fangshuoit.dataobject.ProductCategory;
import com.fangshuoit.dataobject.ProductInfo;
import com.fangshuoit.service.CategoryService;
import com.fangshuoit.service.ProductService;
import com.fangshuoit.utils.ResultVOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 范承波 on 2017/9/26.
 * email：1197691639@qq.com
 * phone：18169015880
 */
@RestController
@RequestMapping(value = "/buyer/product")
public class BuyerProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;


    @GetMapping(value = "/list")
    public ResultVO list() {

        //1、查询所有的上架商品
        List<ProductInfo> productInfoList = productService.findUpAll();

        //2、查询类目（一次性查询）
      /*  List<Integer> categoryTypeList = new ArrayList<>();
        //传统方法
        for (ProductInfo productInfo : productInfoList) {
            categoryTypeList.add(productInfo.getCategoryType());
        }*/
        //精简方法（java8,lambda）
        List<Integer> categoryTypeList = productInfoList.stream()
                .map(e -> e.getCategoryType())
                .collect(Collectors.toList());
        List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(categoryTypeList);


        //3、数据拼装
        List<ProductVO> productVOList = new ArrayList<>();
        for (ProductCategory productCategory : productCategoryList) {
            ProductVO productVO = new ProductVO();
            productVO.setCategoryType(productCategory.getCategoryType());
            productVO.setCategoryName(productCategory.getCategoryName());

            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            for (ProductInfo productInfo : productInfoList) {
                if (productInfo.getCategoryType().equals(productCategory.getCategoryType())) {
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    BeanUtils.copyProperties(productInfo, productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);
        }

       /* ResultVO resultVO = new ResultVO();
        resultVO
*/



       /* ProductInfoVO productInfoVO = new ProductInfoVO();
        productInfoVO.setProductName("牛肉干");
        productInfoVO.setProductPrice(new BigDecimal(2.3));
        productInfoVO.setProductDescription("很好吃的牛肉干");
        productInfoVO.setProductIcon("http://www.baidu.com");
        productInfoVO.setProductId("123456");

        ProductVO productVO = new ProductVO();
        productVO.setCategoryName("热榜");
        productVO.setCategoryType(1);
        productVO.setProductInfoVOList(Arrays.asList(productInfoVO));

        ResultVO resultVO = new ResultVO();
        resultVO.setCode(0);
        resultVO.setMsg("成功");
        resultVO.setData(Arrays.asList(productVO));
        resultVO.setData(productVO);
*/
        return ResultVOUtil.success(productVOList);
    }
}
